package com.fzb.forum4j.dao.impl;

import java.util.Map;

import org.springframework.stereotype.Repository;

import com.fzb.forum4j.dao.api.IBlogDao;
import com.fzb.forum4j.po.Blog;
import com.fzb.forum4j.po.QueryMode;
@Repository("blogDaoImpl")
public class BlogDaoImpl extends HibBaseImpl<Blog> implements IBlogDao {
 
	private static String BASICBLOGMSG="t.user.details.headerFileName as header,t.title as title,t.user.userName as userName,t.pubTime as pubTime,t.cid as id,t.keywords as keywords,t.comments.size as commentSize";
	
	private static String SIMPLEBLOGMSG="t.cid as id,t.blogSort.bsId as bsId,t.title as title,t.content as content,t.keywords as keywords";
	
	@Override
	public void getEntityByEid(QueryMode<Blog> qmd) {
		System.out.println();
		Map<String,String> blog=(Map<String,String>)getSession().createQuery("select new map(t.content as content, "+BASICBLOGMSG+") from Blog t where t.cid=:cid and t.user.userName=:userName").setParameter("userName",qmd.getT().getUser().getUserName()).setParameter("cid", qmd.getId()).uniqueResult();
		if(blog==null){
			qmd.getData().put("404", "内容不存在");
			return;
		}
		qmd.getData().putAll(blog);
		Map<String, Object> next=(Map<String, Object>) getSession().createQuery("select new map(t.cid as id,t.title as title) from Blog t where t.cid>:cid and t.user.userName=:userName").setParameter("userName", qmd.getT().getUser().getUserName()).setParameter("cid",qmd.getId()).setMaxResults(1).uniqueResult();
		Map<String, Object> last=(Map<String, Object>) getSession().createQuery("select new map(t.cid as id,t.title as title) from Blog t where t.cid<:cid and t.user.userName=:userName order by t.cid desc").setParameter("userName", qmd.getT().getUser().getUserName()).setParameter("cid", qmd.getId()).setMaxResults(1).uniqueResult();
		updateAlias(qmd,last,next);
	}

	@Override
	public void getBlogsByUser(QueryMode<Blog> qmd) {
		String qwhere="t.user.userName=:userName";
		QueryDataByQmd(qmd,BASICBLOGMSG,qwhere,"cid",qmd.getT().getUser());
	}

	@Override
	public void getBlogsBySort(QueryMode<Blog> qmd) {
		String qwhere="t.user.userName=user.userName and t.blogSort=:blogSort";
		QueryDataByQmd(qmd,BASICBLOGMSG,qwhere,"cid",qmd.getT());
		
	}
	@Override
	public void getList(QueryMode<Blog> qmd) {
		String qwhere="t.user.userName=user.userName order by t.cid";
		QueryDataByQmd(qmd,BASICBLOGMSG,qwhere,"cid");
		
	}

	@Override
	public void getBlogRecommend(QueryMode<Blog> qmd) {
		String qwhere="t.user.userName=user.userName order by t.cid";
		QueryDataByQmd(qmd,BASICBLOGMSG,qwhere,"cid");
	}

	@Override
	public void getBlogsByQmd(QueryMode<Blog> qmd) {
		QueryDataByQmd(qmd, BASICBLOGMSG, "t.keywords like:keywords order by t.cid desc", "cid", qmd.getT());
	}

	@Override
	public void getSimpleBlogMsgByUserAndCid(QueryMode<Blog> qmd) {
		getSimpleEntityMsg(qmd, SIMPLEBLOGMSG, "t.cid="+qmd.getId()+" and t.user.userName='"+qmd.getT().getUser().getUserName()+"'");
	}
}
 